package com.cheng.io.reader;

import java.io.*;
import java.nio.charset.Charset;

/**
 * 解决FileReader读取文件时乱码问题
 * 使用InputStreamReader设置解码字符集，解决读取文件过程中字符乱码问题。转换字节输入流为字符输入流
 * InputStreamReader是字符输入流。转换流、包装流
 * FileReader是一个包装流
 */
public class FileReaderDecodingTest01 {
    public static void main(String[] args) {
        try (
                // 构造器：InputStreamReader(InputStream in, String charsetName) 指定解码字符集
                InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("io/resources/特性1.txt"),"GBK");
                // Reader reader = new FileReader("io/resources/特性1.txt", Charset.forName("GBK")) 使用多态特性创建对象
        ) {
            char[] chars = new char[4];
            int readCount;
            while ((readCount = inputStreamReader.read(chars)) != -1) {
                System.out.print(new String(chars, 0, readCount));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
